package com.qamaster;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Log_calc implements Runnable {

	String filter;
	String tag;
	ArrayList<String> arr_log;
	Process ps;
	boolean stopped = false;
	public static BufferedReader br;

	public Log_calc(ArrayList<String> arr_log, String filter,
			String tag) {
		this.arr_log = arr_log;
		this.filter = filter;
		this.tag = tag;
	}
	public void dostop(){
		
		try {
			stopped = true;
			ps.destroy();
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void run() {
		String log;
		int set_int = 0;
		double val = 0.00;
		double per = 0.00;
		int curr = 0;
		String format = ("#.##");
		java.text.DecimalFormat df = new java.text.DecimalFormat(format);
		// adb tag:w *:S
		try {
			Thread.sleep(30);

			// ps.destroy();
			Runtime.getRuntime().exec("adb logcat -c");
			ps = Runtime.getRuntime().exec("adb logcat "+tag+":"+filter+" *:S");
			br = new BufferedReader(new InputStreamReader(ps.getInputStream()),20000);

			while (!stopped
					&& ((log = br.readLine()) != null)) {
				for (int i = 0; i < arr_log.size(); i++) {
					if (log.contains(arr_log.get(i))) {
						set_int = Integer.parseInt((Log_dialog.dtm.getValueAt(
								i+1, 1).toString())) + 1;
						Log_dialog.dtm.setValueAt((Integer.parseInt(Log_dialog.dtm.getValueAt(0, 1).toString()))+1, 0, 1);
						Log_dialog.dtm.setValueAt(set_int, i+1, 1);
						for (int j = 0; j < arr_log.size()+1; j++) {
							val = Double.parseDouble((Log_dialog.dtm.getValueAt(j,
									1).toString()));
							per = 100*(val / Integer.parseInt(Log_dialog.dtm.getValueAt(0, 1).toString()));
							Log_dialog.dtm.setValueAt(
									df.format(per), j, 2);

						}
					}
			
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}